package cn.zwx.activiti.example;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.zwx.activiti.example.utils.SecurityUtil;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.TaskInfoQuery;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
class ActivitiExampleApplicationForSingle {

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;


    /**
     * @description 部署工作流
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void loadProcess() {
        securityUtil.logInAs("张三");
        // 部署
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/test_1.bpmn")
                .addClasspathResource("processes/test_1.png").name("监听器触发流程").deploy();
        // 打印信息
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }


    /**
     * @description 启动流程实例
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/6 21:28
     **/
    @Test
    void startProcess() {
        securityUtil.logInAs("root");
        Map<String, Object> variables = new HashMap<>();
        Map<String, Object> user = new HashMap<>();
        user.put("name","root");
        variables.put("user",user);
//        // 直接上级
//        Map<String, Object> leader = new HashMap<>();
//        leader.put("name","XueXue");
//        // 部门经理
//        Map<String, Object> manager = new HashMap<>();
//        manager.put("name","张三");
//        // 财务审核
//        Map<String, Object> finance = new HashMap<>();
//        finance.put("name","李四");
//        // 财务打款
//        Map<String, Object> finance0 = new HashMap<>();
//        finance0.put("name","王五");
//        variables.put("leader",leader);
//        variables.put("manager",manager);
//        variables.put("finance",finance);
//        variables.put("finance0",finance0);


        // 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiantingqi");
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiantingqi",variables);
        // 打印信息
        System.out.println("DeploymentId:"+processInstance.getDeploymentId());
        System.out.println("流程实例id:"+processInstance.getId());
        System.out.println("流程定义id:"+processInstance.getProcessDefinitionId());
    }

    /**
     * @description 查询流程实例 下的审核记录
     * @author zhangwenxue
     * @createTime 2021/5/7 21:10
     **/
    @Test
    void getMyHiProcess() {
        HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        List<HistoricTaskInstance> list = taskInstanceQuery.processInstanceId("0df87d51-b262-11eb-81d6-5c80b6df2b95").orderByTaskCreateTime().desc().list();
        // 打印信息
        for (HistoricTaskInstance taskInstance : list) {
            System.out.println("流程实例id"+taskInstance.getProcessInstanceId());
            System.out.println("任务id:"+taskInstance.getId());
            System.out.println("部署id"+taskInstance.getProcessDefinitionId());
            System.out.println("任务负责人:"+taskInstance.getAssignee());
            System.out.println("任务名称"+taskInstance.getName());
            System.out.println("任务创建时间:"+ DateUtil.format(taskInstance.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            System.out.println("=======================================");
        }
    }
    /**
     * @description 查询流程定义 下的审核记录
     * @author zhangwenxue
     * @createTime 2021/5/7 21:10
     **/
    @Test
    void getMyHiProcess1() {
        HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        List<HistoricTaskInstance> list = taskInstanceQuery.processDefinitionKey("reimburse").orderByTaskCreateTime().desc().list();
        // 打印信息
        for (HistoricTaskInstance taskInstance : list) {
            System.out.println("流程实例id"+taskInstance.getProcessInstanceId());
            System.out.println("任务id:"+taskInstance.getId());
            System.out.println("部署id"+taskInstance.getProcessDefinitionId());
            System.out.println("任务负责人:"+taskInstance.getAssignee());
            System.out.println("任务名称"+taskInstance.getName());
            System.out.println("任务创建时间:"+ DateUtil.format(taskInstance.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            System.out.println("=======================================");
        }
    }
    /**
     * @description 查询我自己的任务
     * @return void
     * @author zhangwenxue
     * @createTime 2021/5/11 20:55
     **/
    @Test
    void queryMyTask(){
        TaskInfoQuery taskQuery = taskService.createTaskQuery();
        List<org.activiti.engine.task.Task> list = taskQuery.processDefinitionKey("reimburse").taskAssignee("张三").orderByTaskCreateTime().asc().list();
        for (org.activiti.engine.task.Task task : list) {
            System.out.println("流程实例id:"+task.getProcessInstanceId());
            System.out.println("流程定义id:"+task.getProcessDefinitionId());
            System.out.println("任务id:"+task.getId());
            System.out.println("任务负责人:"+task.getAssignee());
            System.out.println("任务名称:"+task.getName());
            System.out.println("任务创建时间:"+ DateUtil.format(task.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            System.out.println("=======================================");
            // 完成自己的任务
            Map<String, Object> map = CollectionUtil.newHashMap();
//            // 同意审批
            map.put("audit",Boolean.TRUE);
//////            // 添加报销金额
//                map.put("amount",6000);
//////            // 写入leader名称
//            Map<String, Object> finance = new HashMap<>();
//            finance.put("name","王五");
//            map.put("finance",finance);
//            taskService.complete(task.getId());
            taskService.complete(task.getId(),map);
        }
    }


}
